---
description: "Reference for Nitric's v0 Node.js library - Subscribe a handler to a topic and receive new events for processing."
---

# Node.js - topic.subscribe()

Subscribe a handler to a topic and receive new events for processing.

```javascript
import { topic } from '@nitric/sdk'

const updates = topic('updates')

updates.subscribe(async (ctx) => {
  // Log the provided message
  console.log(ctx.req.json())
})
```

## Parameters

<Properties>
  <Property
    name="middleware"
    required
    type="EventMiddleware or EventMiddleware[]"
  >
    The middleware (code) to be triggered by the topic.
  </Property>
</Properties>

## Examples

### Subscribe to a topic

```javascript
import { topic } from '@nitric/sdk'

const updates = topic('updates')

updates.subscribe(async (ctx) => {
  // Log the provided message
  console.log(ctx.req.json())
})
```

### Notes

- A function may only subscribe to a topic once, if multiple subscribers are required, create them in different functions.
- A function may subscribe to OR publish to a topic but not both
